<template>
  <div o-Container>
    <o-list v-bind:source="list.rule"
      v-bind="{ created: create, exported: output, modified: modify, searched: search }" />
    <o-dialog v-bind:source="panel" v-bind="{ opened: load, closed: load }">
      <template slot="content">
        <o-item v-bind:source="item.rule" v-bind="{ saved: save, backed: back, loaded: load }"></o-item>
      </template>
    </o-dialog>
  </div>
</template>

<script>
export default {
  name: "mms_iot_rule_list", /* 这个name暂时不知道用啥用，根据官方文档说的是方便排错的 */
  data() {
    return {
      octopus: this.$parent.octopus,
      companyID: null,
      list: {
        rule: {
          data: {
            id: 135, // 菜单表的八爪鱼ID
            cellData: [{
              name: "CompanyID",
              width: 300,
              searchKey: "=",
              filterKey: "=",
              // show: false
            },
            { name: "Code", width: 150, searchKey: "LIKE" },
            { name: "Name", width: 150, searchKey: "LIKE" },
            { name: "Status", width: 70, searchKey: "=" },
            { name: "TypeID", width: 100, searchKey: "=" },
            { name: "MqttID", width: 200, searchKey: "=" },
            { name: "PushRole", width: 200 },
            { name: "PushUser", width: 200 },
            { name: "NormalCondition", width: 200 },
            { name: "RemindCondition", width: 200 },
            { name: "WarnCondition", width: 200 },
            { name: "AlarmCondition", width: 200 },
            { name: "CreateTime", width: 160 },
            ],
          }
        },
      },
      item: {
        rule: {
          // 以下参数默认由组件内部创建，但是如果有需要，开发人员可在这里提前设置，组件将按照您的预期渲染。
          // cache: false,
          // control: null,
          // dataExtend: null,
          data: {
            id: 135,
            value: "",
            cellData: null,
          },
          button: [{ class: "warning", click: this.copy, text: "Copy" }]
        },
      },
      panel: {
        visible: false,
      },
    }
  },
  created: function () {
    this.item.rule.data.cellData = [
      { name: "CompanyID" },
      { name: "Status" },
      { name: "OrderTypeID" },
      { name: "Code" },
      { name: "Name" },
      { name: "TypeID" },
      { name: "IsCreateOrder" },
      { name: "PushRole", link: [{ mapping: "CompanyID", value: this.octopus.passport.authorization.company }] },
      { name: "PushUser", link: [{ mapping: "CompanyID", value: this.octopus.passport.authorization.company }] },
      { name: "RangeWeek" },
      { name: "RangeHourStart" },
      { name: "RangeHourEnd" },
      { name: "RangeHours" },
      { name: "MqttID" },
      { name: "NormalCondition" },
      { name: "RemindCondition" },
      { name: "AlarmCondition" },
      { name: "WarnCondition" },
      { name: "AlarmMsg" },
      { name: "RecoveryMsg" },
      // {name: "CreateTime"},
    ];

    this.load();
  },
  methods: {
    create: function () {
      this.item.rule.data.value = null; //空
      this.item.rule.control.load();// 加载
      this.item.rule.button = []
      this.panel.visible = true; //打开
    },
    output: function (data) {
      // 导出以后，回调这里给客户端操作。
      // 核心思想是cellData包含search数据（不管有没有），回传以后发送export指令，后台api会直接返回导出的数据提供下载。
    },
    search: function (cell) {

    },
    modify: function (row) {
      this.companyID = row.CompanyID;
      this.item.rule.data.value = row.ID; //赋值
      this.item.rule.control.load(); //刷新
      this.item.rule.button = [{ class: "warning", click: this.copy, text: "Copy" }]
      this.panel.visible = true; //打开
    },
    // 编辑功能加载完成
    load: function () {
      let url = this.octopus.vue.$route.path;
      // MMS
      if (url.indexOf("MMS/IoT/SensorRule/List") > 0) {
        this.octopus.tools.parseCell(this.item.rule.data, "CompanyID").disabled = false;
      }
      // MDM
      else {
        if (this.item.rule.data.value) {//编辑时
          // this.octopus.tools.parseCell(this.item.rule.data, "CompanyID").defaultValue=this.companyID
        } else {//新增时
          this.octopus.tools.parseCell(this.item.rule.data, "CompanyID").defaultValue = this.octopus.passport.authorization.company.toString();
        }
        this.octopus.tools.parseCell(this.list.rule.data, "CompanyID").show = false;
        this.octopus.tools.parseCell(this.list.rule.data, "CompanyID").filterValue = this.octopus.passport.authorization.company;
      }
    },
    save: function () {
      let params = {
        "type": "SensorRule",
        "id": this.item.rule.data.value //参数
      };
      this.octopus.request.api.post("/ESB/Tools/Transfer/WashRefresh", params) //请求
      // 刷新list
      this.list.rule.control.load();
      this.back(); //返回
    },
    // 拷贝，修改名称重新保存
    copy: function () {
      
      let o = this;
      this.octopus.request.api.post("/Octopus/Engine/Item", {
        id: 135,
        type: "CREATE",
        cellData: this.item.rule.data.cellData
      },
        // 处理回调事件，通过分析data.status.code的状态来实现。
        function (data) {
          if (data.status.code == 200) {
            o.octopus.tips.success("Copy成功。");
            o.list.rule.control.load();
            o.back();
          } else {
            o.octopus.tips.error(data);
          }
        });
    },
    back: function () {
      this.panel.visible = false;
    }
  },
  props: ["oData"] //数据
}
</script>
